import type { RoleAuthUpdateReq, RoleDetailResult, RoleEntityReq, RoleListResult, RolePageReq } from "@/api/types";
import request from "@/utils/http";

/** 创建系统角色 */
function create(data: RoleEntityReq): Promise<Resp<string>> {
    return request({
        url: "/sys/role/create",
        method: "POST",
        data,
    });
}

/** 删除系统角色 */
function remove(id: string): Promise<Resp<null>> {
    return request({
        url: "/sys/role/remove",
        method: "POST",
        data: { id },
    });
}

/** 修改系统角色 */
function update(data: RoleEntityReq): Promise<Resp<null>> {
    return request({
        url: "/sys/role/update",
        method: "POST",
        data,
    });
}

/** 更新角色菜单与权限 */
function updateRolePermissions(data: RoleAuthUpdateReq): Promise<Resp<null>> {
    return request({
        url: "/sys/role/updateRolePermissions",
        method: "post",
        data,
    });
}

/** 查看详情 */
function findById(id: string): Promise<Resp<RoleDetailResult>> {
    return request({
        url: "/sys/role/findById",
        method: "get",
        params: { id },
    });
}

/** 分页查询系统角色 */
function page(data: RolePageReq): Promise<Resp<RoleListResult[]>> {
    return request({
        url: "/sys/role/page",
        method: "post",
        data,
    });
}

/** 获取角色的菜单和权限ID集合 */
function findRolePermissions(roleId: string): Promise<Resp<{ menuIds: string[]; permIds: string[] }>> {
    return request({
        url: "/sys/role/findRolePermissions",
        method: "get",
        params: { roleId },
    });
}

export const roleApi = {
    create,
    remove,
    update,
    updateRolePermissions,
    findById,
    page,
    findRolePermissions,
};
